//
//  LFDataRequest.m
//  CoreDataForHXClient
//
//  Created by Apple on 14-6-27.
//  Copyright (c) 2014年 Lemon. All rights reserved.
//

#import "LFDataRequest.h"

NSString *const kLFDataRequestErrorDomain = @"kLFDataRequestErrorDomain";

@implementation LFDataRequest

+ (void)dataRequestWith:(NSURLRequest *)urlRequest complete:(completeAction)completeAction
{
    dispatch_queue_t curQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(curQueue, ^{
        __block NSData *requestData;
        __block NSError *requestError;
        //__block NSMutableArray *parseArray = @[].mutableCopy;
        dispatch_group_t handGroup = dispatch_group_create();
        dispatch_semaphore_t parseSemaphore = dispatch_semaphore_create(0);
        
        dispatch_block_t requestTask = ^(){
            //进行请求
            requestData = [LFDataRequest urlRequest:urlRequest error:&requestError];
            dispatch_semaphore_signal(parseSemaphore);
        };
        
        dispatch_block_t parseTask = ^(){
            //数据处理
            dispatch_semaphore_wait(parseSemaphore, DISPATCH_TIME_FOREVER);
            if (requestError)
            {
                completeAction(nil, requestError);
            } else if (requestData)
            {
                NSError *jsonError = nil;
                NSDictionary *dicRes = [NSJSONSerialization JSONObjectWithData:requestData options:0 error:&jsonError];
                if (jsonError)
                {
                    completeAction(dicRes, [NSError errorWithDomain:kLFDataRequestErrorDomain
                                                               code:kLFDataRequestPaiseError
                                                           userInfo:@{NSLocalizedDescriptionKey:
                                                                          @"数据解析失败!"}]);
                } else
                {
                    completeAction(dicRes, nil);
                }
            }
            /*
            if (requestData)
            {
                NSError *jsonError = nil;
                NSDictionary *dicRes = [NSJSONSerialization JSONObjectWithData:requestData options:0 error:&jsonError];
                if (!jsonError)
                {
                    NSLog(@"%@", dicRes);
                    if ([[dicRes objectForKey:@"is_success"] boolValue])
                    {
                        NSString *strData = [dicRes objectForKey:@"data"];
                        NSArray *aryData = [NSJSONSerialization JSONObjectWithData:[strData dataUsingEncoding:NSUTF8StringEncoding]
                                                                           options:0 error:&jsonError];
                        if (!jsonError)
                        {
                            if (aryData != nil && aryData.count > 0)
                                [parseArray addObjectsFromArray:aryData];
                        }
                    }
                }
            }
             */
        };
        
        dispatch_group_async(handGroup, curQueue, requestTask);
        dispatch_group_async(handGroup, curQueue, parseTask);
       // dispatch_group_wait(handGroup, DISPATCH_TIME_FOREVER);
        dispatch_group_notify(handGroup, dispatch_get_main_queue(), ^{
            if (completeAction)
            {
                
            }
        });
    });
}

+ (NSData *)urlRequest:(NSURLRequest *)urlRequest error:(NSError **)error
{
    NSData *resData = nil;
    NSError *er = nil;
    resData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&er];
    if (er)
    {
        resData = nil;
        (*error) = [NSError errorWithDomain:er.domain code:er.code userInfo:er.userInfo];
    }
    return resData;
}

@end
